package environment;

import settings.Settings;
import environment.UI;

//import java.lang.Integer;

// for loading from Excel
import java.io.File;
import java.io.IOException;

// for loading from Excel
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class Environment {
	
	int side;
	int cellCount;
	
	UI portal;
	
	String filename = "C:/Users/Jay/workspace/nexus/loadFiles/Book2.xls";
	
	public int [][] GUI; // environment array
	public int [][] agentMap; // agent cell locations
	public int [][] agentArray; // agent map array
	int [][] tempArray;
	
	public Environment() {
		
		this.side = Settings.side;
		this.cellCount = Settings.cellCount;
		
		this.GUI = new int[ side ][ side ];
		this.agentMap = new int[ side ][ 3 ];
		this.agentArray = new int[ side ][ side];
		this.tempArray = new int[ side ][ side ];
			
		// create and activate GUI
		portal = new UI();
		portal.setVisible(true);
		
		// initializing array elements
		for( int i = 0; i < Settings.side; i++ ) {
			for( int j = 0; j < 3; j++ ) {
				
				agentMap[ i ][ j ] = -1;
			}
		}
		
		// initializing array elements
		for( int i = 0; i < side; i++ ) {
			for( int j = 0; j < side; j++ ) {
				
				GUI[ i ][ j ] = -1;
				agentArray[ i ][ j ] = -1;
			}
		}
		
		// load map data from excel file
		try {
			loadMapData( filename );
		} catch ( IOException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// method for loading map data from excel file
	public void loadMapData( String fileName ) throws IOException {
		
		
		int [] uiLoad = new int[ cellCount ];
		
		File inputWorkbook = new File( fileName );
		Workbook w;
		Cell cell;
		
		int counter = 0;
		int cellVal;
		
		try {
			
			w = Workbook.getWorkbook( inputWorkbook );
			Sheet sheet = w.getSheet( 0 );
			
			for( int i = 0; i < side; i++ ) {
				for( int j = 0; j < side; j++ ) {
					
					cell = sheet.getCell( j, i );
					cellVal = Integer.parseInt( cell.getContents() );
					
					GUI[ i ][ j ] = cellVal; // loading environment array
					uiLoad[ counter ] = cellVal; // loading input array for UI mapLoad method
					counter++;
				}
 			}
			
		} catch ( BiffException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// UI method for setting up GUI with excel file data
		portal.mapLoad( uiLoad );
	}
	
	/* method to find agent location, not sure if this is the place to do this
	 *  
	public int[] getAgentLocation( int agentID ) {
		
		int [] agentLoc = new int[ 2 ];
		
		agentLoc[ 0 ] = agentMap[ agentID ][ 1 ];
		agentLoc[ 1 ] = agentMap[ agentID ][ 2 ];
		
		return agentLoc;
	}
	*/
	
	// method for updating GUI with agent moves
	public void executeMove( int agentID, int xVal, int yVal ) {
		
		// calculating past and future cell array indexes
		int pastCell = agentMap[ agentID ][ 1 ] + side * agentMap[ agentID ][ 2 ];
		int futureCell = xVal + side * yVal;
		
		portal.moveUpdate( pastCell, GUI[ agentMap[ agentID ][ 2 ] ][ agentMap[ agentID ][ 1 ] ], futureCell, 0);
		
		// updating agent array values with new cell values
		agentArray[ agentMap[ agentID ][ 2 ] ][ agentMap[ agentID ][ 1 ] ] = -1;
		agentMap[ agentID ][ 1 ] = xVal;
		agentMap[ agentID ][ 2 ] = yVal;
		agentArray[ agentMap[ agentID ][ 2 ] ][ agentMap[ agentID ][ 1 ] ] = agentID;
	}
	
	// method for updating GUI with environment changes
	public void changeEnvironment( int xVal, int yVal, int newColor ) {
		
		GUI[ yVal ][ xVal ] = newColor;
		
		portal.cellUpdate( xVal + side * yVal, newColor);
	}
	
	// method for updating only GUI array with environment changes 
	public void changeGUIOnly( int xVal, int yVal, int newColor ) {
		
		GUI[ yVal ][ xVal ] = newColor;
	}
	
	// method for initially adding an agent to the system
	public void setAgent( int xVal, int yVal, int agentID ) {
		
		agentArray[yVal][xVal] = agentID;
		agentMap[agentID][1] = xVal;
		agentMap[agentID][2] = yVal;
		
		portal.cellUpdate(xVal + side * yVal, 0 ); 
	}
	
	/* merge with setAgent perhaps?
	 * 
	public void createAgent( int xVal, int yVal ) {
		
		int current = 0;
		
		while( agentMap[ current ][ 0 ] != -1 ) {
			
			current++;
		}
		
		agentMap[ current ][ 0 ] = xVal;
		agentMap[ current ][ 1 ] = yVal;
	}
	
	// method for finding nearby friendly agents, should be in a recon algorithm class 
	public ArrayList < Integer > findFriends( int xLoc, int yLoc ) {
		
		ArrayList < Integer > friends = new ArrayList < Integer > ();
		
		for( int i = yLoc - 1;  i < yLoc + 2; i++ ) {
			for( int j = xLoc - 1; j < xLoc + 2; j++ ) {
				
				try{
					
					if( agentArray[ i ][ j ] != -1 && i != yLoc && j != xLoc ) {
					
						friends.add( agentArray[ i ][ j ] );
					}
				}
				catch( ArrayIndexOutOfBoundsException e ) {
					
				}
			}
		}
		
		return friends;
	}
	*/
	
	 
	public static void main( String [] args ) {
		
		Environment now = new Environment();
	}
	
}